# **************************************
# --*-- coding: utf-8 --*--
# @Time    : 2024-11-05
# @Author  : white
# @FileName: second.py
# @Software: PyCharm
# **************************************
import winreg
from tkinter import Button, \
    Variable, DISABLED, Radiobutton, Label
from .option_reg import Reg
from .base import Base
from comon import logger


class QuickAccess(Base):
    def __init__(self, ui):
        super().__init__()
        self.ui = ui
        self.left_frame = self.ui.left_frame
        self.right_frame = self.ui.right_frame
        self.name = "管理快速访问"

        self.key = "HubMode"
        self.data_type = winreg.REG_DWORD
        self.reg_path = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer"
        self.reg_obj = Reg(self.reg_path)
        self.data = 1

        self.ui_init()

    def reg(self, text: str):
        # 1为隐藏，0为显示
        if text == "yes":
            self.reg_obj.create_or_change_key(self.key, 0, self.data_type)
            return True
        else:
            self.reg_obj.create_or_change_key(self.key, 1, self.data_type)
            return False

    def ui_init(self):
        self._button()
        self.button.bind("<Button-1>", self.option)

    def _button(self):
        self.button = Button(self.left_frame, text=self.name)
        self.button.pack(pady=self.ui.pady_upper, anchor="w")

    @Base.clear_frame
    def option(self, event=None):
        if self.button['state'] == DISABLED:
            return
        if not self.is_admin():
            Label(self.right_frame, text="此功能需要管理员权限启动！\n请使用管理权限启动后操作！") \
                .pack()
            return
        self.find_type = Variable(value="no")
        try:
            result = self.reg_obj.read_key(self.key)
            if result[0] == 1:
                self.find_type.set("yes")
        except Exception as e:
            logger.error(e)

        Radiobutton(self.right_frame, text="不显示", variable=self.find_type, value="no") \
            .grid(row=0, column=0, sticky="w")
        Radiobutton(self.right_frame, text="显示", variable=self.find_type, value="yes") \
            .grid(row=0, column=1, sticky="w")

        Button(self.right_frame, text="修改", command=self.submit) \
            .grid(row=2, column=0, sticky="w", pady=self.ui.pady_upper)

    def submit(self, event=None):
        text = self.find_type.get()
        try:
            msg = "展示快速访问！" if self.reg(text) else "不展示快速访问！"
            msg += "重启电脑后生效！"
            self.ui.show_msg(msg)
            logger.info(msg)
        except Exception as e:
            self.ui.show_msg(str(e), "error")
            logger.error(e)
